package com.cloudm.technician.ui.modules.cloudm_box.device_detail

import com.cloudm.technician.data.config.Config
import com.cloudm.technician.data.model.DeviceInfo
import com.cloudm.technician.data.network.CloudmBoxApi
import com.cloudm.technician.extensions.isLicense
import com.cloudm.technician.extensions.isRackId
import com.cloudm.technician.ui.base.RxPresenter

/**
 * 作者: wesley
 * 时间: 2017/12/19
 * 描述: 设备详情的业务逻辑
 */
class DeviceDetailPresenter(private val view: DeviceDetailContract.View,
                            private val cloudmBoxApi: CloudmBoxApi) : RxPresenter(),
        DeviceDetailContract.Presenter {


    override fun buildTitle(flag: Int) {
        when (flag) {
            0 -> view.onAddMachineTitleReceive()
            1 -> {
                view.onBasicMsgTitleReceive()
                view.initUi()
            }
        }
    }

    override fun openMachineBrandActivity(typeId: Int) {
        if (typeId == -1) {
            view.showMachineTypeMsg()
            return
        }
        view.openMachineBrandActivity()
    }

    override fun openMachineModelActivity(typeId: Int, brandId: Int) {
        if (typeId == -1) {
            view.showMachineTypeMsg()
            return
        }
        if (brandId == -1) {
            view.showMachineBrandMsg()
            return
        }
        view.openMachineModelActivity()
    }

    override fun addOrModifyDevice(flag: Int, typeId: Int, brandId: Int, modelId: Int, id: Int,
                                   info: DeviceInfo?) {

        val license = view.getLicense() //车牌号
        if (license == null || license.isNullOrEmpty()) {
            view.showLicenseEmpMsg()
            return
        }
        val deviceName = view.getDeviceName() //设备名称
        if (deviceName == null || deviceName.isNullOrEmpty()) {
            view.showDeviceNameEmpMsg()
            return
        }
        if (deviceName.trim().contains(" ")) {
            view.showDeviceNameContainsBlankMsg()
            return
        }
        if (typeId == -1) {
            view.showMachineTypeMsg()
            return
        }
        if (brandId == -1) {
            view.showMachineBrandMsg()
            return
        }
        if (modelId == -1) {
            view.showMachineModelMsg()
            return
        }
        val rackId = view.getRackId() //机号
        if (rackId == null || rackId.isNullOrEmpty()) {
            view.showRackEmpMsg()
            return
        }
        if (!rackId.isRackId()) {
            view.showRackFormatErrMsg()
            return
        }
        val workTime = view.getWorkTime() //工作时长
        if (workTime == null || workTime.isNullOrEmpty()) {
            view.showWorkTimeMsg()
            return
        }
        val factoryTime = view.getFactoryTime() //出厂时间
        if (factoryTime == null || factoryTime.isNullOrEmpty()) {
            view.showFactoryTimeMsg()
            return
        }
        val map = hashMapOf("license" to license, "deviceName" to deviceName,
                "typeId" to typeId.toString(), "brandId" to brandId.toString(),
                "modelId" to modelId.toString(), "rackId" to rackId, "workTime" to workTime,
                "factoryTime" to factoryTime)

        when (flag) {
            0 -> {
                addDevice(map)
            }

            1 -> {
                when (info) {
                    null -> {
                        view.showDeviceEmpMsg()
                    }

                    else -> {
                        info.license = license
                        info.deviceName = deviceName
                        info.typeId = typeId
                        info.brandId = brandId
                        info.modelId = modelId
                        info.rackId = rackId
                        info.workTime = workTime
                        info.factoryTime = factoryTime
                        info.id = id
                        map.put("deviceId", id.toString())
                        editDevice(map, info)
                    }
                }
            }
        }
    }

    override fun addDevice(map: HashMap<String, String>) {
        view.showAddDeviceDialog()
        request(cloudmBoxApi.addDevice(map), view, onSuccess = { info, _ ->
            view.hideDeviceDialog()
            view.addDeviceSuccess(info)
        }, onFailure = { errorMsg ->
            view.hideDeviceDialog()
            view.showErrorMsg(errorMsg)
        })
    }

    override fun editDevice(map: HashMap<String, String>, info: DeviceInfo) {
        view.showEditDeviceDialog()
        request(cloudmBoxApi.editDevice(map), view, onSuccess = { _, _ ->
            view.hideDeviceDialog()
            view.editDeviceSuccess(info)
        }, onFailure = { errorMsg ->
            view.hideDeviceDialog()
            view.showErrorMsg(errorMsg)
        })
    }


    override fun dealWithScanResult(value: String?) {
        if (value == null || value.isNullOrEmpty()) {
            view.showScanResultEmpMsg()
            return
        }
        val license = when (value.contains(Config.LICENSE_PREFIX)) {
            true -> value.substringAfterLast(Config.LICENSE_PREFIX)
            else -> value
        }

        if (!license.isLicense()) {
            view.showLicenseFormatMsg()
            return
        }

        view.showRightLicense(license)
    }
}